with ada.text_io;
with ada.characters.handling; use ada.characters.handling;
package body long_ints is
    
    package t_io renames ada.text_io;

    function get_long_int return LongInt is
	i,j : Integer;
	c : Character;
	a : LongInt;
    begin
	t_io.put("Enter an integer (max 31 digits) ");
	
	for i in MaxInt'range loop
	    a(i) := '0';
	end loop;
	
	i := 1;
	loop
	    if i = MaxInt'last then
		t_io.skip_line;
		t_io.put("Input too long, truncated to ");
		t_io.put(a(1..MaxInt'last-1));
		t_io.new_line;
		exit;
	    end if;
	    t_io.get(c);
	    if is_digit(c) then
		a(i) := c;
		i := i + 1;
	    end if;
	    exit when t_io.end_of_line;
	end loop;
	j := MaxInt'last-i+1;
	for i in j..MaxInt'last-1 loop
	    a(MaxInt'last-i+j) := a(MaxInt'last-i);
	end loop;
	for i in 1..j loop
	    a(i) := '0';
	end loop;
	return a;
    end get_long_int;

    procedure add_long_ints(num1: in LongInt; num2 : in LongInt) is
	a,b,tmp : Integer;
	sum : LongInt;
    begin
	tmp := 0;
	for i in reverse MaxInt'range loop
	    a := Character'pos(num1(i)) - Character'pos('0');
	    b := Character'pos(num2(i)) - Character'pos('0');
	    sum(i) := Character'val( Character'pos('0') + ((a+b+tmp) REM 10));
	    tmp := (a+b+tmp) / 10;
	end loop;
	t_io.new_line;
	print_long_int(num1);
	t_io.new_line;
	print_long_int(num2);
	t_io.new_line;
	print_dashes(sum);
	t_io.new_line;
	print_long_int(sum);
	t_io.new_line;
    end add_long_ints;

    procedure print_long_int(num : in LongInt) is
	isspace : boolean;
    begin
	isspace := true;
	for i in MaxInt'range loop
	    if isspace and num(i) /= '0' then
		isspace := false;
	    end if;
	    if isspace then
		t_io.put(' ');
	    else
		t_io.put(num(i));
	    end if;
	end loop;
    end print_long_int;

    procedure print_dashes(num : in LongInt) is
	isspace : boolean;
    begin
	isspace := true;
	for i in MaxInt'range loop
	    if isspace and num(i) /= '0' then
		isspace := false;
	    end if;
	    if isspace then
		t_io.put(' ');
	    else
		t_io.put('-');
	    end if;
	end loop;
    end print_dashes;
end long_ints;
	   
